package com.bruno.tecnoagenda;

import com.bruno.tecnoagenda.data.Person;
import com.bruno.tecnoagenda.data.PersonContainer;
import com.vaadin.data.Property;
import com.vaadin.terminal.ExternalResource;
import com.vaadin.ui.Component;
import com.vaadin.ui.Link;
import com.vaadin.ui.Table;

/**
 * Tabela para exibição dos contatos
 * @author bssantos
 *
 */
public class PersonList extends Table {
    public PersonList(TecnoAgenda app) {
        setSizeFull();
        setContainerDataSource(app.getDataSource());
        setVisibleColumns(PersonContainer.NATURAL_COL_ORDER);
        setColumnHeaders(PersonContainer.COL_HEADERS_ENGLISH);
        /*
         * Make table selectable, react immediatedly to user events, and pass events to the
         * controller (our main application)
         */
        setSelectable(true);
        setImmediate(true);
        setColumnCollapsingAllowed(true);
        setColumnReorderingAllowed(true);
        addListener((Property.ValueChangeListener) app);
        /* We don't want to allow users to de-select a row */
        setNullSelectionAllowed(false);
        
        // customize email column to have mailto: links using column generator
        addGeneratedColumn("email", new ColumnGenerator() {
            public Component generateCell(Table source, Object itemId,
                    Object columnId) {
                Person p = (Person) itemId;
                Link l = new Link();
                l.setResource(new ExternalResource("mailto:" + p.getEmail()));
                l.setCaption(p.getEmail());
                return l;
            }
        });
  }

}
